package Lee_code;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class Lee_77 {
    List<List<Integer>> res = new ArrayList<>();
    int target_len;
    int max_end;

    public List<List<Integer>> combine(int n, int k) {

        ArrayList<Integer> list = new ArrayList<Integer>();
        target_len = k;
        max_end = n;
        Backtrace(list, 1);

        return res;
    }

    private void Backtrace(List<Integer> list, int start_index) {
        if (list.size() == target_len) {
            res.add(new ArrayList<>(list));
            return;
        }
        for (int i = start_index; i <= max_end; i++) {
            list.add(i);
            Backtrace(list, start_index + 1);
            list.remove(list.size() - 1);
        }
    }
}

